package cn.alex.sso.project.mapper;

import cn.alex.sso.project.entity.SysUserRole;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Alex
 * @description 针对表【sys_user_role(用户和角色关联表)】的数据库操作Mapper
 * @createDate 2024-09-24 17:05:38
 * @Entity cn.alex.sso.project.entity.SysUserRole
 */
public interface SysUserRoleMapper extends BaseMapper<SysUserRole> {

    /**
     * 根据用户ID获取角色ID列表
     *
     * @param userId 用户ID
     * @return 角色ID列表
     */
    default List<Long> getRoleIdListByUserId(Long userId) {
        if (ObjUtil.isEmpty(userId)) {
            return ListUtil.empty();
        }
        List<SysUserRole> userRoleList = this.selectList(new LambdaQueryWrapper<SysUserRole>()
                .eq(SysUserRole::getUserId, userId)
        );
        return userRoleList.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
    }

    /**
     * 根据角色ID获取用户ID列表
     *
     * @param roleIdList 角色ID集合
     * @return 用户ID列表
     */
    default List<Long> getUserIdListByRoleId(List<Long> roleIdList) {
        if (CollUtil.isEmpty(roleIdList)) {
            return ListUtil.empty();
        }
        List<SysUserRole> roleClientList = this.selectList(new LambdaQueryWrapper<SysUserRole>()
                .in(SysUserRole::getRoleId, roleIdList)
        );
        return roleClientList.stream().map(SysUserRole::getUserId).distinct().collect(Collectors.toList());
    }
}




